package com.dashu.base.utils.http.token;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.dashu.base.redis.JedisUtil;
import com.dashu.base.redis.RedisLiveKeyConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * Token的工具类
 *
 * @author zhous
 * @since 2018-2-23 14:01:41
 */
public class TokenTools {

    /**
     * 生成token放入session
     *
     * @param request
     * @param tokenServerkey
     */
    public static String createToken(HttpServletRequest request, String tokenServerkey, String userId) {
        String token = TokenProccessor.getInstance().makeToken(userId);
        JedisUtil.hmset(RedisLiveKeyConstants.TOKEN, tokenServerkey, token);
        return token;
    }

    /**
     * 移除token
     *
     * @param request
     * @param tokenServerkey
     */
    public static void removeToken(HttpServletRequest request, String tokenServerkey) {
        request.getSession().removeAttribute(tokenServerkey);
    }

    /**
     * 判断请求参数中的token是否和session中一致
     *
     * @param request
     * @param tokenClientkey
     * @param tokenServerkey
     * @return
     */
    public static boolean judgeTokenIsEqual(HttpServletRequest request, String tokenClientkey, String tokenServerkey) {
        String token_client = request.getHeader(tokenClientkey);
        if (StringUtils.isEmpty(token_client)) {
            return false;
        }
        String token_server =    JedisUtil.hget(RedisLiveKeyConstants.TOKEN, tokenServerkey);
        if (StringUtils.isEmpty(token_server)) {
            return false;
        }

        if (!token_server.equals(token_client)) {
            return false;
        }

        return true;
    }

}